// pages/info/info.js
const app = getApp()
var t = require('./../../utils/request.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    username: '',
    sms: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  taskUsername: function (e) {
    this.setData({
      username: e.detail
    })
  },

  pay: function () {
    console.log()

    if (!this.data.username) {
      wx.showToast({
        icon: 'warn',
        title: '请填写您的姓名',
      })
      return;
    }

    if (!this.data.sms) {
      wx.showToast({
        icon: 'warn',
        title: '请填写您的联系方式',
      })
      return;
    }

    t.toPost('/minip/user/testpay', {
      pays: 9.9,
      body: 'Jack购机体验券',
      uid: wx.getStorageSync('userInfo').id,
      openid: wx.getStorageSync('userInfo').openId,
      uname: this.data.username,
      phone: this.data.sms
    }, (res) => {
      wx.requestPayment({
        timeStamp: res.timeStamp,
        nonceStr: res.nonceStr,
        package: res.package,
        signType: 'MD5',
        paySign: res.paySign,
        success(res) {
          console.log(res, '支付回调结果')
          wx.redirectTo({
            url: '/pages/prize/prize?paystatus=1',
          })
        },
        fail(res) {
          wx.showToast({
            title: '支付已取消',
          })
        }
      })
      console.log(res, 111)
    })
  },

  getPhoneNumber: function (e) {
    var that = this

    wx.login({
      success: function (res) {
        var param = {
          code: res.code,
          enc: e.detail.encryptedData,
          iv: e.detail.iv,
          uid: that.data.userInfo.id
        }
        t.toPost('/minip/user/phone', param, (res) => {
          console.log(res, 234)
          if (res.code === '000000') {
            that.setData({
              sms: res.data
            })
          }
        }, (err) => {
          console.log(err)
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
    * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: '不用剪线头的杰克A5，邀您9.9元抢500体验券！',
      path: '/pages/index/index',
      imageUrl: 'http://qny.zhiniugroup.com/blog/20190106162150.png',
      success: function (res) {
        console.log(res, '分享成功')
      }
    }
  }
})